hotbasicfandomcom-20200213-history
HotBasic for Beginners - Chapter 1
HotBasic for New Coders Even if you have never written a line of code, you can program now in HotBasic! Chapter One - The basics, by K5HUM I am not a professional programmer. I am strictly a hobbyist. Therefore because I am a hobbyist, I think I am well suited to write a tutorial that is specifically aimed at the neophytes. That is, write something in a style that other coding novices can understand without resorting to rampant code geek speak. To begin I want to explain why you should start learning with HotBasic. The reason is simple. No matter how you progress, you will never find another language that has the simple architecture and performance that will out pace HotBasic! It matters not what your future criteria might be: compile speeds, size of executables, execution speed, or ease of use. Although HotBasic (hereafter referred to as HB) comes in a free version as well as a paid version, this tutorial is based on the paid or Penthouse version. The difference is that the free version will not produce GUI programs. For the modest cost I sincerely suggest you commit to purchase the Penthouse version. However, to encourage new users to HB's Penthouse version, the code presented here can be compiled in real time with HB's free web compiler at: http://204.188.170.69/hotcomp.html The Web compiler is limited to 800 lines of input code, but for evaluation purposes is more than adequate. HB can create several types of binary files. The type of file most of you will be interested in is the program or executable GUI file that present a standard graphical Windows. A Window that contains all these Objects (buttons etc) is called a Form. Contained in these Window forms are typically objects like buttons and other controls for the user to manipulate the program. These Objects or controls frequently have Events associated with them that point to and activate code in Sub routines. The code contained within a Sub or Function is the Method by which things will get done. When you write code to create a button on a Form, you can optionally associate an Event with the Button so when its clicked with a mouse (or the mouse equivalent keys are pressed) some code is run. This code is typically contained in a Procedure or Sub routine. Sub routines can be of two general types. Procedures that do a job and exit, and Functions that run code and return information directly back to the calling point. As you progress in HB coding you will eventually stop using the Create command for the more robust and flexible Dim or Dimension instruction. More on this in later chapters. Take a look at the structure of this tiny HotBasic Program. NOTE: an hypen "'" starts a comment. '---begin code--------- $APPTYPE GUI '---the type of output file we want (an EXE) Declare Sub Saythis '---code will contain this subroutine CREATE Form as Form '---a window object is created Create B1 as Button '---a button is created inside the form Onclick = Saythis '---buttons Event is to run sub Saythis End Create '---end of buttons properties End Create '---end of Form create Form.ShowModal '---instruct the OS to display the Form Sub Saythis '---begin Saythis subroutine SHOWMESSAGE "Hello World!" '---a method to display text End Sub '---close this sub. '---end code----------- You can write code like HELLOWORLD.BAS above with any utility that can produce ordinary text files, such as Notepad (Notepad comes supplied with all versions of windows). Eventually you will want to use a more sophisticated programmers editor such as an IDE or Integrated Development Environment. Note the few code line offsets to improve clarity of reading. In addition source (code) files like the one illustrated above must end with the file extension .BAS to be recognized by the HB compiler. Before we discuss how to compile this source into a Windows executable file using HB, lets talk about something called program flow and the IP or Instruction Pointer. When HELLOWORLD is loaded into memory, the IP constantly (for practical purposes) moves between all Objects in the Form that have Events associated with them, looking to see IF an event has occurs (like a button click). If this happens, the IP then jumps to the subroutine associated with that event. The code in that sub routine is executed and then the IP returns to the loop monitoring for other Form object events. Although overly simplistic, this explanation is essentially accurate in light of how the Windows Operating system works using a system of messages than can be intercepted and acted upon. Ok, at this point you have a source file called helloworld.bas, now, how is it turned into an executable file? In HB this is a two step process. First (and behind the scenes) the HB source is converted into assembler style Object code; then the Object code is linked into the actual machine or executable code. HB Penthouse comes with several files. We will focus on two, HOT.EXE (the HB compiler), HOTBASIC.LIB (HB's Library of functions etc). In addition to these two you need LINK.EXE (an object code linker) which is public domain and available free. Lets assume you have placed HOT.EXE in a folder called \HOTBASIC and created a BIN sub folder (\HOTBASIC\BIN) with LINK.EXE in it. To compile your code, you pass your source to the compiler as a command tail. i.e. "\PATH2HB\HOT.EXE HELLOWORLD.BAS" Next, HOT.EXE will look for its library file, then the linker (LINK.EXE) in the \BIN sub directory of HB's folder (such as \HOTBASIC\BIN). These actions can be done automatically with a BATCH file OR you can do it from the Start/Run menu by typing: "\PATH2HB\HOT.EXE HELLOWORLD.BAS" less quotes. If everything described went successfully, you will find "HELLOWORLD.EXE" in the same folder containing HOT.EXE Note: HB's web site provides a new standard installation utility called HBSETUP.EXE that will essentially do all folder creation and file moves for you, including downloading automatically all the essential HB tools. More on this later. LETS REVIEW: A GUI program begins with the creation (or Dimensioning) of a Form or Forms populated with Objects in each. (Note: Control is an alias for Objects). These Objects can have Events associated with them that trigger code in Sub routines. The code in Subs are Methods to accomplish various tasks). The program begins execution when it reaches the Form.ShowModal statement. At this point the Instruction Pointer loops through all the controls over and over until an Event fires. Normally all Procedures and Functions follow the ShowModal statement. These techniques gave rise to the acronym OOP for Object Oriented Programming.